/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package ModuloConfiguracion;

import Modelo.DAL.Horario_DAO;
import Acceso.Security;
import Modelo.Horario;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.apache.struts.action.ActionErrors;
import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;
import org.apache.struts.action.ActionMessage;

/**
 *
 * @author josmary
 */
public class modificarHorarioAction extends org.apache.struts.action.Action {

    /* forward name="success" path="" */
    private static final String SUCCESS = "success";
    private static final String FAILURE = "failure";
    private static final String[] ALLOWED = {"Administrador", "Gestionar Horarios"};

    /**
     * This is the action called from the Struts framework.
     * @param mapping The ActionMapping used to select this instance.
     * @param form The optional ActionForm bean for this request.
     * @param request The HTTP Request we are processing.
     * @param response The HTTP Response we are processing.
     * @throws java.lang.Exception
     * @return
     */
    @Override
    public ActionForward execute(ActionMapping mapping, ActionForm form,
            HttpServletRequest request, HttpServletResponse response)
            throws Exception {


        if (!Security.validateSession(request, ALLOWED)) {
            Security.privilegeError(response);
            return mapping.findForward("errorPriv");
        }

        ActionErrors errors = new ActionErrors();
        modificarHorarioForm formBean = (modificarHorarioForm) form;

        String id = formBean.getId();

        String hora_inicio = formBean.getHora_inicio();
        String minutos_inicio = formBean.getMinutos_inicio();
        String meridiano_inicio = formBean.getMeridiano_inicio();

        String hora_fin = formBean.getHora_fin();
        String minutos_fin = formBean.getMinutos_fin();
        String meridiano_fin = formBean.getMeridiano_fin();


        if (id == null || id.equals("")) {
            errors.add("horario_no_actualizado", new ActionMessage("errors.horario_no_actualizado"));
            saveErrors(request, errors);
            request.setAttribute("idHorario", id);
            return mapping.findForward(FAILURE);
        }

        Horario h = new Horario();
        h.setHorainicioS(hora_inicio + ":" + minutos_inicio + " " + meridiano_inicio);
        h.setHorafinS(hora_fin + ":" + minutos_fin + " " + meridiano_fin);
        System.out.println("Horarios con el mismo patron:");
        System.out.println(Horario_DAO.consultarHorarios(h).toString());
        String other = Horario_DAO.consultarHorarios(h).getString("tam");
        if (other!=null && Integer.parseInt(other) > 0) {
            errors.add("horario_repetido", new ActionMessage("errors.horario_repetido"));
            saveErrors(request, errors);
            return mapping.findForward(FAILURE);
        }

        int idHorario = Integer.parseInt(id);
        if (!Horario_DAO.actualizarHorario(idHorario, hora_inicio, minutos_inicio, meridiano_inicio,
                hora_fin, minutos_fin, meridiano_fin)) {
            request.setAttribute("idHorario", id);
            return mapping.findForward(FAILURE);
        }
        errors.add("horario_actualizado", new ActionMessage("errors.horario_actualizado"));
        saveErrors(request, errors);
        return mapping.findForward(SUCCESS);
    }
}
